package com.aosgame;

import com.androidgame.fx.Game;
import com.androidgame.fx.Graphics;
import com.androidgame.fx.Input.TouchEvent;
import com.androidgame.fx.Screen;

public class PlayScreen extends Screen {

	public static final	int	MINION_MAX = 10;
	
	private Map			map;
	private Player		player;
	private DrtPad		drtPad;
	private AttackPad	attackPad;
	private Minion[]	minions;
	private Graphics	g;
	
	public PlayScreen(Game game) {
		super(game);
		
		g = game.getGraphics();
		Assets.map = g.newBitmap("logo.jpg");
		map = new Map(Assets.map);
		
		Assets.player = g.newBitmap("player.png");
		player = new Player();
		player.init(Assets.player);
		map.setFocus(player);
		
		Assets.troll = g.newBitmap("troll.png");
		minions = new Minion[MINION_MAX];
		for(int i = 0; i < MINION_MAX; i++) {
			minions[i] = new Minion(Assets.troll);
			minions[i].init();
			minions[i].setTarget(player);
		}
		
		Assets.drtPad = g.newBitmap("drtpad.png");
		Assets.drtStick = g.newBitmap("drtstick.png");
		drtPad = new DrtPad(player);

		Assets.attackPad = g.newBitmap("attackpad.png");
		Assets.onAttackPad = g.newBitmap("onattackpad.png");
		attackPad = new AttackPad(player, minions);
		
		Assets.target = g.newBitmap("target.png");
	}

	@Override
	public boolean touchDown(TouchEvent event) {
		drtPad.touchDown(event);
		attackPad.touchDown(event);
		
		return false;
	}

	@Override
	public boolean touchDragged(TouchEvent event) {
		drtPad.touchDragged(event);
		attackPad.touchDragged(event);
		
		return false;
	}

	@Override
	public boolean touchUp(TouchEvent event) {
		drtPad.touchUp(event);
		attackPad.touchUp(event);
		
		return false;
	}
	
	@Override
	public void update(float deltaTime) {
		player.update();
		
		map.update();
		
		for(int i = 0; i < MINION_MAX; i++) {
			if(minions[i].alive) {
				minions[i].update();
			}
		}
	}

	@Override
	public void present(float deltaTime) {
		map.present(g);
		
		if(player.target != null) {
			int left, top;
			int scrWidth = 1280;
			int scrHeight = 720;
			int frameWidth = 64;
			int frameHeight = 32;
			if(player.position.x < scrWidth / 2) {
				left = player.target.position.x % scrWidth - frameWidth / 2;
			} else if(player.position.x > 3840 - scrWidth / 2) {
				left = player.target.position.x % scrWidth - frameWidth / 2;
			} else {
				left = scrWidth / 2 + player.target.position.x - player.position.x - frameWidth / 2;
			}
			
			if(player.position.y < scrHeight / 2) {
				top = player.target.position.y % scrHeight;
			} else if(player.position.y > 3600 - scrHeight / 2) {
				top = player.target.position.y % scrHeight;
			} else {
				top = scrHeight / 2 + player.target.position.y - player.position.y;
			}
			
			g.drawBitmap(Assets.target, left, top + 20);
		}
		
		for(int i = 0; i < MINION_MAX; i++) {
			if(map.screenRect.contains(minions[i].position.x, minions[i].position.y)) {
				if(minions[i].position.y <= player.position.y) {
					minions[i].present(g, player);	
				}
			}
		}
		
		player.present(g);
		
		for(int i = 0; i < MINION_MAX; i++) {
			if(map.screenRect.contains(minions[i].position.x, minions[i].position.y)) {
				if(minions[i].position.y > player.position.y) {
					minions[i].present(g, player);
				}
			}
		}
		
		drtPad.present(g);
		attackPad.present(g);
		game.getGameTime().present(g);
	}

	@Override
	public void pause() {
		
	}

	@Override
	public void resume() {
		
	}

	@Override
	public void dispose() {
		
	}
}
